﻿namespace Digitalmes.Application.Sys.Roles.Queries;

/// <summary>
/// 通过角色Id查找查询角色指令，其中会返回结果会包含角色与菜单映射关系。
/// </summary>
/// <param name="RoleId"></param>
public record GetRoleByIdQuery(long RoleId) : IRequest<IResult<SysRoleDto?>>;

internal sealed class GetRoleByIdQueryHandler(IRepository<SysRole> repo) : IRequestHandler<GetRoleByIdQuery, IResult<SysRoleDto?>>
{
    public async Task<IResult<SysRoleDto?>> Handle(GetRoleByIdQuery request, CancellationToken cancellationToken)
    {
        var role = await repo.AsQueryable()
            .Includes(s => s.RoleMenus)
            .FirstOrDefaultAsync(s => s.Id == request.RoleId, cancellationToken);
        return Result.From(role.Adapt<SysRoleDto?>());
    }
}
